/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.coupon.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.common.security.annotation.Inner;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.entity.CnCouponLog;
import com.ykht.proj.coupon.service.CnCouponLogService;
import com.ykht.proj.coupon.vo.CnCouponLogVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


/**
 * 优惠券领取记录表
 *
 * @author proj code generator
 * @date 2020-11-16 17:00:21
 */
@RestController
@AllArgsConstructor
@RequestMapping("/cncouponlog")
@Api(value = "cncouponlog", tags = "优惠券领取记录表管理")
@Slf4j
public class CnCouponLogController {

	private final CnCouponLogService cnCouponLogService;

	/**
	 * 分页查询
	 *
	 * @param page          分页对象
	 * @param cnCouponLogVo 优惠券领取记录表
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	public R getCnCouponLogPage(Page page, CnCouponLogVo cnCouponLogVo) {
		return cnCouponLogService.findPage(page, cnCouponLogVo);
	}


	/**
	 * 通过id查询优惠券领取记录表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	public R<CnCouponLogVo> getById(@PathVariable("id") Long id) {
		log.info("->通过id查询优惠券领取记录表 {}", id);
		return R.ok(cnCouponLogService.getByIdCnCouponLog(id));
	}

	/**
	 * 新增优惠券领取记录表
	 *
	 * @param cnCouponLog 优惠券领取记录表
	 * @return R
	 */
	@ApiOperation(value = "新增优惠券领取记录表", notes = "新增优惠券领取记录表")
	@SysLog("新增优惠券领取记录表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('coupon_cncouponlog_add')")
	public R save(@RequestBody CnCouponLog cnCouponLog) {
		return R.ok(cnCouponLogService.save(cnCouponLog));
	}

	/**
	 * 修改优惠券领取记录表
	 *
	 * @param cnCouponLog 优惠券领取记录表
	 * @return R
	 */
	@ApiOperation(value = "修改优惠券领取记录表", notes = "修改优惠券领取记录表")
	@SysLog("修改优惠券领取记录表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('coupon_cncouponlog_edit')")
	public R updateById(@RequestBody CnCouponLog cnCouponLog) {
		return R.ok(cnCouponLogService.updateById(cnCouponLog));
	}

	/**
	 * 通过id删除优惠券领取记录表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除优惠券领取记录表", notes = "通过id删除优惠券领取记录表")
	@SysLog("通过id删除优惠券领取记录表")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('coupon_cncouponlog_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(cnCouponLogService.removeById(id));
	}

	/**
	 * 核销
	 */
	@PostMapping("writeOff")
	@ApiOperation(value = "通过核销码核销优惠券", notes = "通过核销码核销优惠券")
	public R writeOff(@RequestBody CnCouponLog cnCouponLog) {
		return cnCouponLogService.writeOff(cnCouponLog);
	}

	@GetMapping("findByCode")
	@ApiOperation(value = "验证", notes = "验证")
	public R findByCode(String verificationCode) {
		return cnCouponLogService.findByCode(verificationCode);
	}

	/**
	 * 获取C端用户是否是新客户（品牌/门店）
	 */
	@PostMapping("getBrandShopCustomer")
	@ApiOperation(value = "获取C端用户是否是新客户（品牌/门店）", notes = "获取C端用户是否是新客户（品牌/门店）")
	public List<CnCouponLog> getBrandShopCustomer(@RequestBody CnCouponLog cnCouponLog) {
		QueryWrapper<CnCouponLog> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(CnCouponLog::getRecipientId, cnCouponLog.getRecipientId())
				.eq(null != cnCouponLog.getBrandId(), CnCouponLog::getBrandId, cnCouponLog.getBrandId())
				.eq(null != cnCouponLog.getShopId(), CnCouponLog::getShopId, cnCouponLog.getShopId())
				.eq(CnCouponLog::getStatus, 1);
		return cnCouponLogService.list(queryWrapper);
	}
	@Inner(false)
	@GetMapping("sendMsgFlag")
	@ApiOperation(value = "判断是否需要发送订阅消息")
	public Boolean sendMsgFlag( @RequestParam("couponId") Long couponId, @RequestParam("personId")Long personId) {
		List<CnCouponLog> list = null;
		try {
			list = cnCouponLogService.list(Wrappers.lambdaQuery(CnCouponLog.class).eq(CnCouponLog::getCouponId, couponId)
					.between(CnCouponLog::getExpireTime, LocalDateTime.now(), LocalDateTime.now().plusDays(3))
					.eq(CnCouponLog::getStatus, 0).eq(CnCouponLog::getRecipientId, personId));
			return list.size()>0?true:false;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 优惠券领取记录表 审核
	 *
	 * @param cnCouponLog 优惠券领取记录表
	 * @return R
	 */
	@ApiOperation(value = "优惠券领取记录表 审核", notes = "优惠券领取记录表 审核")
	@SysLog("优惠券领取记录表 审核")
	@PostMapping("updateCheckStatus")
	public R updateCheckStatus(@RequestBody CnCouponLog cnCouponLog) {
		log.info("->优惠券领取记录表 审核{}", cnCouponLog);
		cnCouponLog.setUpdateUser(SecurityUtils.getUserId());
		cnCouponLog.setUpdateTime(LocalDateTime.now());
		cnCouponLog.setCheckTime(LocalDateTime.now());
		cnCouponLog.setCheckUser(SecurityUtils.getUserId());
		cnCouponLogService.updateCheckStatus(cnCouponLog);
		return R.ok();
	}


	/**
	 * 优惠券领取记录表 反馈状态
	 *
	 * @param cnCouponLog 优惠券领取记录表
	 * @return R
	 */
	@ApiOperation(value = "优惠券领取记录表 反馈状态", notes = "优惠券领取记录表 反馈状态")
	@SysLog("优惠券领取记录表 反馈状态")
	@PostMapping("updateFeedbackStatus")
	public R updateFeedbackStatus(@RequestBody CnCouponLog cnCouponLog) {
		log.info("->优惠券领取记录表 反馈状态{}", cnCouponLog);
		cnCouponLog.setUpdateUser(SecurityUtils.getUserId());
		cnCouponLog.setUpdateTime(LocalDateTime.now());
		cnCouponLog.setFeedbackTime(LocalDateTime.now());
		cnCouponLogService.updateFeedbackStatus(cnCouponLog);
		return R.ok();
	}


}
